package smlab.view.ui.button
{
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.Point;

	/**
	 * 按钮类
	 * @author simon
	 *
	 */
	public class Button_SM extends Sprite
	{
		protected var _skin:MovieClip;
		protected var _skinX:Number; //皮肤的原始坐标
		protected var _skinY:Number; //皮肤的原始坐标

		/**
		 * 记录按钮原本的滤镜效果，以便还原按钮灰掉效果
		 */
		protected var _skinFilters:Array;
		protected var _enable:Boolean=true;
		protected var _defaultSkin:MovieClip;
		protected var _addChildSkin:Boolean;

		/**
		 * 构造函数
		 * @param btnSkin		按钮皮肤（必须 4 帧的 MovieClip，不传值则自动生成默认皮肤）
		 * @param _addChildSkin	是否把皮肤 addChild 到组件内，如 true 则皮肤将从原来的父容器中移除
		 * @param w				按钮宽度(这个是给默认皮肤用的)
		 * @param h				按钮高度(这个是给默认皮肤用的)
		 *
		 */
		public function Button_SM(btnSkin:MovieClip=null, addChildSkin:Boolean=true, w:Number=60, h:Number=24)
		{
			super();
			_addChildSkin=addChildSkin;
			if (btnSkin == null)
			{
				_defaultSkin=new MovieClip;
				_defaultSkin.graphics.beginFill(0);
				_defaultSkin.graphics.drawRect(0, 0, w, h);
				skin=_defaultSkin;
			}
			else
			{
				skin=btnSkin;
			}
		}

		/**
		 * 设置按钮皮肤
		 * 必须为4帧的 Movieclip
		 * 第1帧-鼠标未经过；第1帧-鼠标经过；第3帧-鼠标压下；第4帧-不可点击状态【这里会自动添加一个灰度滤镜，如不想添加可以用 enable$CanClick方法】）
		 * @param mc
		 *
		 */
		public function set skin(mc:MovieClip):void
		{
			if (_defaultSkin)
			{
				if (this.contains(_defaultSkin))
				{
					this.removeChild(_defaultSkin);
					_defaultSkin=null;
				}
			}
			_skin=mc;
			_skin.mouseChildren=false;
			_skinFilters=_skin.filters.concat();
			_skin.addEventListener(Event.ADDED_TO_STAGE, onAddToStage);
			_skin.addEventListener(Event.REMOVED_FROM_STAGE, onRemoveFromStage);
			_skin.gotoAndStop(1);
			skinX=mc.x;
			skinY=mc.y;
			if (_addChildSkin)
			{
				mc.x=0;
				mc.y=0;
				var oldParent:DisplayObjectContainer=mc.parent;
				addChild(mc);
				this.x=skinX;
				this.y=skinY;
				oldParent.addChild(this);
			}
			this.buttonMode=true;
			_skin.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver$);
			_skin.addEventListener(MouseEvent.ROLL_OVER, onMouseOver$);
			_skin.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown$);
			_skin.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut$);
			_skin.addEventListener(MouseEvent.ROLL_OUT, onMouseOut$);
			_skin.addEventListener(MouseEvent.MOUSE_UP, onMouseUp$);
		}

		public function get skin():MovieClip
		{
			return _skin;
		}

		public function set skinX(n:Number):void
		{
			_skinX=n;
		}

		public function get skinX():Number
		{
			return _skinX;
		}

		public function set skinY(n:Number):void
		{
			_skinY=n;
		}

		public function get skinY():Number
		{
			return _skinY;
		}

		private function onAddToStage(e:Event):void
		{
			if (skin.stage != null)
			{
				skin.stage.addEventListener(Event.MOUSE_LEAVE, onMouseLeave);
			}
			skin.addEventListener(Event.DEACTIVATE, onDeactivate);
		}

		private function onDeactivate(e:Event):void
		{
			onMouseOut$(null);
		}

		private function onMouseLeave(e:Event):void
		{
			onMouseOut$(null);
		}

		private function onRemoveFromStage(e:Event):void
		{
			if (skin.stage != null)
			{
				skin.stage.removeEventListener(Event.MOUSE_LEAVE, onMouseLeave);
			}
			skin.removeEventListener(Event.DEACTIVATE, onDeactivate);
		}

		override public function set buttonMode(b:Boolean):void
		{
			skin.buttonMode=b;
		}

		override public function get buttonMode():Boolean
		{
			return _skin.buttonMode;
		}

		/**
		 * 设置按钮是否可用
		 * @param b
		 * @param setGrey
		 *
		 */
		public function set enable(b:Boolean):void
		{
			_enable=b;
			this.mouseEnabled=b;
			this.mouseChildren=b;
			this.buttonMode=b;

			setGrey(!b);
			skin.gotoAndStop(_enable ? 1 : 4);
		}

		public function get enable():Boolean
		{
			return _enable;
		}

		private function setGrey(b:Boolean):void
		{
			if (b)
			{
				var mat:Array=[0.3086, 0.6094, 0.082, 0, 0, 0.3086, 0.6094, 0.082, 0, 0, 0.3086, 0.6094, 0.082, 0, 0, 0, 0, 0, 1, 0];
				var colorMat:ColorMatrixFilter=new ColorMatrixFilter(mat);
				skin.filters=[colorMat];
			}
			else
			{
				skin.filters=_skinFilters;
			}
		}

		/**
		 * 为让 button 停留在第4帧时可点击且可调整灰度滤镜而多加的一个方法（如无特殊需求请用 enable()）
		 * @param goto4Frame				走到第4帧
		 * @param canClickAndShowCursor     是否显示手型
		 * @param setGrey     				是否将按钮设置灰色滤镜
		 *
		 */
		public function enable$CanClick(goto4Frame:Boolean, enable$:Boolean, setGrey$:Boolean=false):void
		{
			_enable=enable$;
			this.mouseEnabled=enable$;
			this.mouseChildren=enable$;
			this.buttonMode=enable$;
			skin.gotoAndStop(goto4Frame ? 4 : 1);
			setGrey(setGrey$);
		}

		private var _isGotoAndStop:Boolean=false; //

		public function gotoAndStopAt(n:uint, alwayStop:Boolean=false):void
		{
			if (_enable)
			{
				_isGotoAndStop=alwayStop;
				skin.gotoAndStop(n);
			}
		}

		protected function onMouseOver$(e:MouseEvent):void
		{
			if (!_isGotoAndStop)
			{
				if (_enable)
				{
					skin.gotoAndStop(2);
				}
			}
		}

		protected function onMouseDown$(e:MouseEvent):void
		{
			if (_enable)
			{
				_isGotoAndStop=false;
				skin.gotoAndStop(3);
			}
		}

		protected function onMouseOut$(e:MouseEvent):void
		{
			if (!_isGotoAndStop)
			{
				if (_enable)
				{
					skin.gotoAndStop(1);
				}
			}
		}

		protected function onMouseUp$(e:MouseEvent):void
		{
			if (!_isGotoAndStop)
			{
				if (_enable)
				{
					skin.gotoAndStop(2);
				}
			}
		}

	}
}